package array;

import array.Ex128III.Solution;

public class Ex169 {
    public int majorityElement(int[] nums) {
        if (nums == null || nums.length == 0) return -1;
        int count = 1, last = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != last) {
                count--;
            } else {
                count++;
            }
            if (count == 0) {
                count = 1;
                last = nums[i];
            }
        }

        return last;
    }

    public static void main(String[] args) {
        Ex169 e = new Ex169();
        int[] nums = new int[]{2,1,2,4,2,2,1};
        int i = e.majorityElement(nums);
        System.out.println(i);
    }
}
